package com.voicechat.firstmet.imcontrol;


import android.util.Log;

import com.tencent.ilivesdk.ILiveCallBack;
import com.tencent.ilivesdk.core.ILiveLoginManager;
import com.voicechat.firstmet.IMStatusObservable;

/**
 * Created by valexhuang on 2018/4/4.
 */

public class LoginHelper implements ILiveLoginManager.TILVBStatusListener {
    private ILoginView loginView;
    private boolean mLoginState;
    private String mCurrentAccount;
    private String TAG = "LoginHelper";

    public LoginHelper(ILoginView loginView) {
        this.loginView = loginView;
    }
    public LoginHelper() {
    }
    public static LoginHelper newInstance() {
        LoginHelper fragment = new LoginHelper();
        return fragment;
    }

    /**
     * 登录
     *
     * @param account
     */
    public void login(final String account,String userSig) {
        Log.i(TAG, "login: --------isLogin()--------"+isLogin());
        //判断是否已登录
        if (isLogin()) {
            //已登录并当前用户id与传入用户id一致的，执行注销操作
            if (account.equals(mCurrentAccount)) {
                ILiveLoginManager.getInstance().iLiveLogout(new ILiveCallBack() {
                    @Override
                    public void onSuccess(Object data) {
                        Log.i(TAG, "iLiveLogout------onSuccess: --------------"+data);
                        mLoginState = false;
                        mCurrentAccount = null;
                        loginView.onLogoutSDKSuccess();
                        IMStatusObservable.getInstance().deleteObserver(LoginHelper.this);
                    }
                    @Override
                    public void onError(String module, int errCode, String errMsg) {
                        Log.i(TAG, "iLiveLogout------onError: --------------"+errMsg);
                        loginView.onLogoutSDKFailed(errCode, errMsg);
                    }
                });
            }
            //切换账号登录前，需先注销先前登录用户
            else {
                System.out.println("请先注销" + mCurrentAccount);
            }
        } else {
            /**
             *
             * 此处请自行参考文档注册用户account并生成userSig进行测试
             * 文档地址：https://gitee.com/vqcloud/doc_demo/blob/master/%E5%BC%80%E5%8F%91%E5%89%8D%E5%BF%85%E7%9C%8B/%E5%9F%BA%E6%9C%AC%E6%A6%82%E5%BF%B5.md#%E7%94%A8%E6%88%B7%E7%AD%BE%E5%90%8D
             *
             */
            try {
                ILiveLoginManager.getInstance().iLiveLogin(account, userSig, new ILiveCallBack() {
                    @Override
                    public void onSuccess(Object data) {
                        Log.i(TAG, "onSuccess: ------iLiveLogin----"+data);
                        mLoginState = true;
                        loginView.onLoginSDKSuccess();
                        //登录成功后监听用户在线状态
                        IMStatusObservable.getInstance().addObserver(LoginHelper.this);
                        ILiveLoginManager.getInstance().setUserStatusListener(IMStatusObservable.getInstance());
                        mCurrentAccount = account;
                    }
                    @Override
                    public void onError(String module, int errCode, String errMsg) {
                        Log.i(TAG, "onError: ------iLiveLogin----"+errMsg);
                        mLoginState = false;
                        loginView.onLoginSDKFailed(errCode, errMsg);
                    }
                });
            }catch (Exception e){
                Log.i(TAG, "login: -----------");
            }
        }


    }

    /**
     * 离线时间处理
     *
     * @param error
     * @param message
     */
    @Override
    public void onForceOffline(int error, String message) {
        mLoginState = false;
        mCurrentAccount = null;
        loginView.updateLoginState(false);
    }
    public boolean isLogin() {
        return mLoginState;
    }

   public interface LoginOut{
        // 登出成功
        void onLoginOutSuccess();
        // 登出失败
        void onLoginOutFailed(int code, String desc);
    }


}
